<script setup lang="ts">
import {
  createMedicalOrder,
  getAddressList,
  getMedicalOrderPre
} from '@/services/order'
import type { AddressItem, OrderPre } from '@/types/order'
import OrderMedical from './components/OrderMedical.vue'
import { showToast } from 'vant'
import { computed } from 'vue'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
const route = useRoute()
// 获取地址列表
const addresssList = ref<AddressItem[]>([])
const getList = async () => {
  const res = await getAddressList()
  addresssList.value = res.data
}
// 获取药品订单
const orderPre = ref<OrderPre>()
const getDetail = async () => {
  const res = await getMedicalOrderPre(route.query.id as string)
  orderPre.value = res.data
}
getList()
getDetail()

// 默认地址或第0项地址
const showAddress = computed(() => {
  const o = addresssList.value.find((item) => item.isDefault) //默认地址
  if (o) {
    return o
  } else {
    return addresssList.value[0]
  }
})

const isAgree = ref(false)
const orderId = ref('')
const show = ref(false)
const submit = async () => {
  // 1. 支付协议同意
  if (!isAgree.value) return showToast('请勾选支付协议')
  // 2. 判断处方id
  if (!orderPre.value?.id) return showToast('处方异常')
  // 3. 地址id
  if (!showAddress.value.id) return showToast('请选择收货地址')
  // 4. 发送请求生成订单id
  if (!orderId.value) {
    const res = await createMedicalOrder({
      // couponId: orderPre.value.couponId,
      addressId: showAddress.value.id,
      id: orderPre.value?.id
    })
    orderId.value = res.data.id
  }
  // 5. 展示一个抽屉
  show.value = true
}
</script>

<template>
  <cp-pay-sheet
    v-model:show="show"
    :order-id="orderId"
    :actual-payment="orderPre?.actualPayment"
    callback="/order/pay/result"
  >
  </cp-pay-sheet>
  <div class="order-pay-page" v-if="showAddress && orderPre">
    <cp-nav-bar title="药品支付" />
    <div class="order-address">
      <p class="area">
        <van-icon name="location" />
        <span>{{
          showAddress.province + showAddress.city + showAddress.county
        }}</span>
      </p>
      <p class="detail">{{ showAddress.addressDetail }}</p>
      <p>
        {{ showAddress.receiver }}
        {{ showAddress.mobile.replace(/(\d{3})\d*(\d{4})/, '$1****$2') }}
      </p>
    </div>
    <OrderMedical :medicines="orderPre.medicines"></OrderMedical>
    <div class="order-detail">
      <van-cell-group>
        <van-cell
          title="药品金额"
          :value="`￥${orderPre?.payment.toFixed(2)}`"
        />
        <van-cell
          title="运费"
          :value="`￥${orderPre?.expressFee.toFixed(2)}`"
        />
        <van-cell
          title="优惠券"
          :value="`￥${orderPre?.couponDeduction.toFixed(2)}`"
        />
        <van-cell
          title="实付款"
          :value="`￥${orderPre?.pointDeduction.toFixed(2)}`"
          class="price"
        />
      </van-cell-group>
    </div>
    <div class="order-tip">
      <p class="tip">
        由于药品的特殊性，如非错发、漏发药品的情况，药品一经发出
        不得退换，请核对药品信息无误后下单。
      </p>
      <van-checkbox v-model="isAgree"
        >我已同意<a href="javascript:;">支付协议</a></van-checkbox
      >
    </div>
    <van-submit-bar
      :price="orderPre!.payment * 100"
      button-text="立即支付"
      button-type="primary"
      text-align="left"
      @click="submit"
    ></van-submit-bar>
  </div>
</template>

<style lang="scss" scoped>
:deep(.van-nav-bar) {
  background-color: var(--cp-primary);
  .van-nav-bar__arrow,
  .van-nav-bar__title {
    color: #fff;
  }
}
:deep(.van-cell) {
  .van-cell__title {
    font-size: 16px;
  }
  .van-cell__value {
    font-size: 16px;
  }
  &.price {
    .van-cell__value {
      font-size: 18px;
      color: var(--cp-price);
    }
  }
}
:deep(.van-submit-bar) {
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.1);
  .van-button {
    width: 200px;
  }
}
.order-pay-page {
  padding: 46px 0 65px;
}
.order-address {
  padding: 15px 15px 0 15px;
  background-color: #fff;
  font-size: 13px;
  .area {
    color: var(--cp-tag);
    margin-bottom: 5px;
    .van-icon-location {
      color: #ff7702;
      font-size: 14px;
    }
  }
  .detail {
    font-size: 17px;
    margin-bottom: 5px;
  }
  &::after {
    content: '';
    display: block;
    height: 12px;
    background-color: var(--cp-bg);
    margin: 0 -15px;
    margin-top: 15px;
  }
}

.order-tip {
  padding: 0 15px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 10px;
  .tip {
    font-size: 12px;
    color: var(--cp-tag);
    width: 100%;
    &::before {
      content: '*';
      color: var(--cp-price);
      font-size: 14px;
    }
    margin-bottom: 30px;
  }
  .van-checkbox {
    a {
      color: var(--cp-primary);
    }
  }
}
</style>
